﻿using System;
using DimsMapper.Modules;

namespace DimsMapper
{
    class TilesSet
    {
        //[StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct Tile
        {
            public UInt16 roof;
            public UInt16 floor;

            public override string ToString()
            {
                return "Roof: " + roof.ToString() + " Foor:" + floor.ToString();
            }
        }

        Tile[] tiles; // массив плиток (второй уровень начинается с 10000 индекса, третий уровень с 20000)

        public TilesSet(DBFile file, int tilesSize)
        {
            tiles = new Tile[tilesSize];

            // Загрузка блока тайлов
            for (int i = 0; i < tilesSize; i++)
            {
                tiles[i].roof = file.ReadShort();
                tiles[i].floor = file.ReadShort();
            }
        }

        public int GetRoofID(int index)
        {
            return tiles[index].roof;
        }

        public int GetFloorID(int index)
        {
            return tiles[index].floor;
        }

        public int GetRoofID(int x, int y, int level)
        {
            y = ((level - 1) * 100 + y) * 100;
            return tiles[x + y].roof;
        }

        public int GetFloorID(int x, int y, int level)
        {
            y = ((level - 1) * 100 + y) * 100;
            return GetFloorID(x + y);
        }

        public int SetRoofID(int x, int y, int level, int fid)
        {
            y = ((level - 1) * 100 + y + 3) * 100;
            return tiles[(2 + x) + y].roof = (ushort)(fid & 0x3FFF);
        }

        public int SetFloorID(int x, int y, int level, int fid)
        {
            y = ((level - 1) * 100 + y) * 100;
            return tiles[x + y].floor = (ushort)(fid & 0x3FFF);
        }

        public void SetFloorRegion(int x1, int y1, int x2, int y2, int level, int fid)
        {
            int _x = Math.Min(x1, x2);
            int _y = Math.Min(y1, y2);
            int sx = Math.Max(x1, x2);
            int sy = Math.Max(y1, y2);

            //var r = new Random();

            for (int y = _y; y <= sy; y++)
            {
                for (int x = _x; x <= sx; x++)
                {
                    //if (r.Next(0, 3) > 0) continue;

                    SetFloorID(x, y, level, fid);
                }
            }
        }

        public void SetRoofRegion(int x1, int y1, int x2, int y2, int level, int fid)
        {
            int _x = Math.Min(x1, x2);
            int _y = Math.Min(y1, y2);
            int sx = Math.Max(x1, x2);
            int sy = Math.Max(y1, y2);

            for (int y = _y; y <= sy; y++)
            {
                for (int x = _x; x <= sx; x++)
                {
                    SetRoofID(x, y, level, fid);
                }
            }      
        }

        public void AddElevation()
        {
            Tile[] tmp = tiles;

            int size = tiles.Length + 10000;
            tiles = new Tile[size];

            tmp.CopyTo(tiles, 0);
        }
    }
}
